package com.idc.excel.netflowsnmp;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.alibaba.fastjson.JSON;

public class NetflowSnmpDeviation {

	public static void main(String[] args) {

		Map<String, Map<String, NetflowSnmpReadDto>> dataMap = new LinkedHashMap<>();

		Set<String> provinceSet = new LinkedHashSet<>();

		Set<String> keySet = new HashSet<>();

		keySet.add("IDC#IDC-CR上联163中继电路");
		keySet.add("IDC#IDC-CR上联CN2中继电路");
		keySet.add("IDC#IDC-CR上联省汇聚中继电路");
		keySet.add("城域网#MAN-CR上联163中继电路");
		keySet.add("城域网#MAN-CR上联CN2中继电路");
		keySet.add("城域网#MAN-CR上联省汇聚中继电路");

		BigDecimal thousand = new BigDecimal(1000);

		EasyExcel.read("C:\\Users\\" + System.getProperty("user.name") + "\\Desktop\\CR\\originData.xlsx",
				NetflowSnmpReadDto.class, new ReadListener<NetflowSnmpReadDto>() {

			@Override
			public void invoke(NetflowSnmpReadDto data, AnalysisContext context) {
				String province = data.getProvince();
				if (StringUtils.isEmpty(province)) {
					return;
				}
				provinceSet.add(province);
				String type = data.getType();
				String key = type + "#" + data.getCircuitProperties();
				if (!keySet.contains(key)) {
					return;
				}
				Map<String, NetflowSnmpReadDto> provinceMap = dataMap.get(type);
				if (null == provinceMap) {
					provinceMap = new LinkedHashMap<>();
					dataMap.put(type, provinceMap);
				}
				NetflowSnmpReadDto dto = provinceMap.get(province);
				if (null == dto) {
					provinceMap.put(province, data);
				} else {
					dto.setOutflow(dto.getOutflow().add(data.getOutflow()));
					dto.setSnmpOut(dto.getSnmpOut().add(data.getSnmpOut()));
				}
			}

			@Override
			public void doAfterAllAnalysed(AnalysisContext context) {

			}
		}).sheet(0).doRead();
		dataMap.values().forEach(value -> {
			value.values().forEach(v -> {
				System.out.println(JSON.toJSONString(v));
			});
		});
		Map<String, NetflowSnmpReadDto> idcMap = dataMap.get("IDC");
		Map<String, NetflowSnmpReadDto> manMap = dataMap.get("城域网");
		List<NetflowSnmpExportDto> exports = new ArrayList<>();
		provinceSet.forEach(province -> {
			NetflowSnmpReadDto idcDto = idcMap.get(province);
			if (null == idcDto) {
				idcDto = new NetflowSnmpReadDto();
			}
			BigDecimal idcNetflow = idcDto.getOutflow().divide(thousand, 4, RoundingMode.HALF_UP);
			BigDecimal idcSnmp = idcDto.getSnmpOut().divide(thousand, 4, RoundingMode.HALF_UP);
			BigDecimal idcDeviation = new BigDecimal(0);
			if (idcSnmp.doubleValue() > 0) {
				idcDeviation = idcSnmp.subtract(idcNetflow).divide(idcSnmp, 4, RoundingMode.HALF_UP);
			}
			NetflowSnmpReadDto manDto = manMap.get(province);
			if (null == manDto) {
				manDto = new NetflowSnmpReadDto();
			}
			BigDecimal manNetflow = manDto.getOutflow().divide(thousand, 4, RoundingMode.HALF_UP);
			BigDecimal manSnmp = manDto.getSnmpOut().divide(thousand, 4, RoundingMode.HALF_UP);
			BigDecimal manDeviation = new BigDecimal(0);
			if (manSnmp.doubleValue() > 0) {
				manDeviation = manSnmp.subtract(manNetflow).divide(manSnmp, 4, RoundingMode.HALF_UP);
			}
			NetflowSnmpExportDto exportDto = new NetflowSnmpExportDto(province, idcSnmp, idcNetflow, idcDeviation, manSnmp, manNetflow, manDeviation);
			exports.add(exportDto);
		});
		EasyExcel.write("C:\\Users\\" + System.getProperty("user.name") + "\\Desktop\\xxx.xlsx",
				NetflowSnmpExportDto.class).sheet("Sheet1").doWrite(exports);
	}

}
